package shajie.shajun.mmhf.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import shajie.shajun.mmhf.R;

/**
 * Created by admin on 2018/1/28.
 */

public class Spinner_Adapter extends ArrayAdapter<String> {
    private Context mContext;
    private String[] mStringArray;
    public Spinner_Adapter(@NonNull Context context, String[] resource) {
        super(context, android.R.layout.simple_spinner_dropdown_item,resource);
        this.mContext=context;
        this.mStringArray=resource;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //修改Spinner展开后的字体颜色
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent,false);
        }

        //此处text1是Spinner默认的用来显示文字的TextView
        TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
        tv.setText(mStringArray[position]);
        tv.setTextSize(20f);
        tv.setTextColor(Color.WHITE);
        tv.setBackgroundColor(mContext.getResources().getColor(R.color.blackgray));
        return convertView;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 修改Spinner选择后结果的字体颜色
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
        }

        //此处text1是Spinner默认的用来显示文字的TextView
        TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
        tv.setText(mStringArray[position]);
        tv.setTextSize(20f);
        tv.setTextColor(Color.WHITE);
        tv.setBackgroundColor(mContext.getResources().getColor(R.color.blackgray));
        return convertView;
    }
    public void setStringArray(String[] stringArray){
        mStringArray=stringArray;
        notifyDataSetChanged();
    }
    public String getStringPostion(int position){
        return  mStringArray[position];
    }

    @Override
    public int getCount() {
        return mStringArray.length;
    }
}
